package com.googlecode.moneyManager.excelReader;

import com.googlecode.moneyManager.config.SpringApplicationContext;
import com.googlecode.moneyManager.model.Currency;
import com.googlecode.moneyManager.model.TypeSecurityValue;
import com.googlecode.moneyManager.service.SecurityValueService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.SessionFactory;

import java.io.FileInputStream;

/**
 * Author: Sergiy Doroshenko
 * Date: Apr 26, 2010
 * Time: 11:19:34 PM
 */

public class Reader {
    public static void main(String[] args) {
        SecurityValueService service = SpringApplicationContext.getBean(SecurityValueService.class);
        SessionFactory sessionFactory =  SpringApplicationContext.getBean(SessionFactory.class);
       // Session session = sessionFactory.getCurrentSession();
       // session.beginTransaction();
        
        try {

            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row;
            XSSFCell cell;

//            int rows; // No of rows
//            rows = sheet.getPhysicalNumberOfRows();
//
//            int cols = 0; // No of columns
//            int tmp = 0;
//
//            // This trick ensures that we get the data properly even if it doesn't start from first few rows
//            for(int i = 0; i < 10 || i < rows; i++) {
//                row = sheet.getRow(i);
//                if(row != null) {
//                    tmp = sheet.getRow(i).getPhysicalNumberOfCells();
//                    if(tmp > cols) cols = tmp;
//                }
//            }
//
//            for(int r = 0; r < rows; r++) {
//                row = sheet.getRow(r);
//                if(row != null) {
//                    for(int c = 0; c < cols; c++) {
//                        cell = row.getCell(c);
//                        if(cell != null) {
//                            System.out.println(cell);
//                        }
//                    }
//                }
//            }
            String baksPart = sheet.getRow(0).getCell(7).getRawValue();
            String evroPart = sheet.getRow(1).getCell(7).getRawValue();
            String grivnaPart = sheet.getRow(2).getCell(7).getRawValue();

            String baksRate = sheet.getRow(0).getCell(9).getRawValue();
            String evroRate = sheet.getRow(1).getCell(9).getRawValue();

            
            String grivnaAll = sheet.getRow(4).getCell(7).getRawValue();
            String baksAll = sheet.getRow(5).getCell(7).getRawValue();
            System.out.println(baksAll);
            System.out.println(grivnaAll);
            System.out.println(baksPart);
            System.out.println(grivnaPart);
            System.out.println("evroPart=" + evroPart);
            System.out.println("baksRate = " + baksRate);
            System.out.println(evroRate);

            service.addValue(baksAll, TypeSecurityValue.ALL, Currency.USD);
            service.addValue(grivnaAll,TypeSecurityValue.ALL,Currency.UAH);
            service.addValue(baksPart, TypeSecurityValue.PART, Currency.USD);
            service.addValue(evroPart, TypeSecurityValue.PART, Currency.EUR);
            service.addValue(grivnaPart,TypeSecurityValue.PART,Currency.UAH);
            service.addValue(baksRate,TypeSecurityValue.RATE,Currency.USD);
            service.addValue(evroRate, TypeSecurityValue.RATE, Currency.EUR);
            
           // session.getTransaction().commit();
        } catch(Exception ioe) {

            //if(!session.getTransaction().wasCommitted())
           //     session.getTransaction().rollback();
            ioe.printStackTrace();
        }
//        }finally {
//            session.close();
//        }

    }
}
